package com.wain.util;

import cn.hutool.core.io.FileUtil;

import java.util.regex.Matcher;

import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.util.ReUtil;
import com.wain.enums.RegexEnum;
import cn.hutool.core.lang.func.Func1;

import java.io.File;
import java.util.List;
import java.util.regex.Pattern;

public class MainEntry {
    public static void main(String[] args) {
        String sourcePath = "C:\\Users\\ｐｃ\\Desktop\\tooliTest\\TOOLi - コピー\\TOOLiZ_CommonService\\src\\jp\\co\\trc\\tooliz\\service\\common\\dao\\table";  // 文件?路径，替????的文件?路径
        String targetPath = "C:\\Users\\ｐｃ\\Desktop\\tooliTest\\TooLiFinish3";
        // 查找符合条件的文件
        List<File> fileList = FileUtil.loopFiles(new File(sourcePath), file -> {
            // 只保留以DTO.java尾的文件
            return file.getName().endsWith("DTO.java");
        });
        // 找到的文件
        for (File inputFile : fileList) {
            String absolutePath = inputFile.getAbsolutePath();
            System.out.println(absolutePath);
            // 指定输出出文件路径
            String[] dir = absolutePath.split("src");

            String outputFilePath = targetPath + File.separator + "src" + File.separator + dir[1] + File.separator + inputFile.getName();
            File outputFile = FileUtil.file(outputFilePath);


            FileUtil.mkParentDirs(outputFile);
            String content = FileUtil.readUtf8String(inputFile);
            // 正则表达式匹配，并删除匹配成功的内容
            String result = replaceALL(content, RegexEnum.GET_METHOD,inputFile);
            result = replaceALL(result, RegexEnum.GET_FINAL_METHOD,inputFile);
            result = replaceALL(result, RegexEnum.SET_METHOD,inputFile);
            result = replaceALL(result, RegexEnum.SET_FINAL_METHOD,inputFile);
            FileUtil.writeUtf8String(result, outputFile);
            System.out.println(inputFile.getName() + ":内容匹配成功，已删除匹配成功的内容。");
        }
    }

    private static String replaceALL(String content, RegexEnum regexEnum,File file) {
        String result = ReUtil.replaceAll(content, PatternPool.get(regexEnum.getRegex()), (Func1<Matcher, String>) matcher -> {
            if (regexEnum.equals(RegexEnum.valueOf("GET_METHOD")) || regexEnum.equals(RegexEnum.valueOf("GET_FINAL_METHOD"))) {
                // TRUE返回原始内容 FALSE ""
                return checkGetMethod(matcher, regexEnum,file) ? "" : matcher.group();
            }
            return checkSetMethod(matcher, regexEnum,file) ? "" : matcher.group();

        });
        return result;
    }


    private static Boolean checkGetMethod(Matcher matcher, RegexEnum regexEnum,File file) {
        // 获取分组内容
        String group = matcher.group();
        // 再次解析并对方法名和return结果校验
        Pattern pattern = Pattern.compile(regexEnum.getRegex_child());
        Matcher MatcherChild = pattern.matcher(group);
        MatcherChild.find();
        String group1 = MatcherChild.group(1);
        String group2 = MatcherChild.group(2);
        try {
            if (group1.equalsIgnoreCase(group2.split("\\.")[1])) {
                return true;
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            if (group1.equalsIgnoreCase(group2)) {
                return true;
            }
        }
        System.err.println("no_delete_get:" + group+":"+file.getName());
        return false;
    }

    private static Boolean checkSetMethod(Matcher matcher, RegexEnum regexEnum,File file) {
        // 获取分组内容
        String group = matcher.group();
        // 再次解析并对方法名和return结果校验
        Pattern pattern = Pattern.compile(regexEnum.getRegex_child());
        Matcher MatcherChild = pattern.matcher(group);
        MatcherChild.find();
        String group1 = MatcherChild.group(1);
        String group2 = MatcherChild.group(2);
        String group3 = MatcherChild.group(3);
        String group4 = MatcherChild.group(4);
        String[] split = group2.split("\\s");
        if (group1.equalsIgnoreCase(group3.split("\\.")[1]) && split[split.length - 1].equals(group4)) {
            return true;
        }
        System.err.println("no_delete_set:" + group+":"+file.getName());
        return false;
    }


}
